from __future__ import division
from sklearn.metrics import confusion_matrix
import pickle
import sys
import numpy as np


def load_pickle(file_name):
    return pickle.load(open(file_name, 'rb'))


def get_confusion_matrix(target, pred):
    return confusion_matrix(target, pred)


if __name__ == '__main__':
    model_file = sys.argv[1]
    data_file = sys.argv[2]
    model = load_pickle(model_file)

    x, target = load_pickle(data_file)
    pred = model.predict(x)

    print('class,' + ','.join(model.classes_))
    conf_mat = get_confusion_matrix([i.strip() for i in target], [i.strip() for i in pred])
    shape = conf_mat.shape
    k = shape[0]

    tp = [conf_mat[i][i] for i in range(k)]

    truth = np.sum(conf_mat, 1)
    predict = np.sum(conf_mat, 0)
    for e, t in enumerate(conf_mat):
        c = model.classes_[e]
        print(c + ',' + ','.join([str(i) for i in t]))

    print('tp,' + ','.join([str(t) for t in tp]))
    print('truth,' + ','.join([str(t) for t in truth]))
    print('predict,' + ','.join([str(t) for t in predict]))

    c = sum([1 if k == v else 0 for k, v in zip([i.strip() for i in target], [i.strip() for i in pred])])
    print(c / len(target))
